<template>
	<view class="pages">
		<!-- 		<view class="pages_title">
			Welcome !
		</view> -->
		<!-- 		<view class="items">
			<view class="itemsTip">
				<span>*</span>
				<span>{{$t('Username1')}}</span>
			</view>
			<view class="sd">
				<u--input :placeholder="$t('Pleaseentercontent')" border="surround" v-model="FormData.real_name"
					:customStyle="{width:'650rpx',height:'49rpx'}" clearable></u--input>
			</view>
		</view> -->
		<!-- 上传头像 -->
		<view class="touxiang">
			<div class="touxiangleft">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="1" :uploadText="$t('uploadText')">
				</u-upload>
			</div>

		</view>
		<view class="items">
			<view class="itemsTip">
				<span>*</span>
				<!-- <span>Surname</span> -->
				{{$t('register1.fristname')}}
			</view>
			<view class="sd">
				<u--input :placeholder="$t('Pleaseentercontent')" border="surround" v-model="FormData.lastname"
					:customStyle="{width:'650rpx',height:'49rpx'}" clearable></u--input>
			</view>
		</view>
		<view class="items">
			<view class="itemsTip">
				<span>*</span>
				<!-- <span>First Name</span> -->
				<span>
					<!-- First Name -->
					{{$t('register1.LastName')}}
				</span>

			</view>
			<view class="sd">
				<u--input :placeholder="$t('Pleaseentercontent')" border="surround" v-model="FormData.firstname"
					:customStyle="{width:'650rpx',height:'49rpx'}" clearable></u--input>
			</view>
		</view>
		<view class="items">
			<view class="itemsTip">
				<span>*</span>
				<!-- <span>Country</span> -->
				<span>{{$t('Countries')}}</span>

			</view>
			<view class="sd">
				<u--input :placeholder="$t('Pleaseentercontent')" border="surround" v-model="FormData.country"
					:customStyle="{width:'650rpx',height:'49rpx'}" clearable></u--input>
			</view>
		</view>

		<view class="items">
			<view class="itemsTip">
				<span>*</span>
				<!-- <span>Address 1</span> -->
				<span>{{$t('Profile.Address')}}</span>

			</view>
			<view class="sd">
				<u--input :placeholder="$t('Pleaseentercontent')" border="surround" v-model="FormData.address"
					:customStyle="{width:'650rpx',height:'49rpx'}" clearable></u--input>
			</view>
		</view>
		<view class="items">
			<view class="itemsTip">
				<span>*</span>
				<!-- <span>Address 2</span> -->
				<span>{{$t('Address2')}}</span>

			</view>
			<view class="sd">
				<u--input :placeholder="$t('Pleaseentercontent')" border="surround" v-model="FormData.address2"
					:customStyle="{width:'650rpx',height:'49rpx'}" clearable></u--input>
			</view>
		</view>
		<view class="items">
			<view class="itemsTip">
				<span>*</span>
				<!-- <span>State</span> -->
				<span>{{$t('State')}}</span>

			</view>
			<view class="sd">
				<u--input :placeholder="$t('Pleaseentercontent')" border="surround" v-model="FormData.state"
					:customStyle="{width:'650rpx',height:'49rpx'}" clearable></u--input>
			</view>
		</view>
		<view class="items">
			<view class="itemsTip">
				<span>*</span>
				<!-- <span>City</span> -->
				<span>{{$t('City')}}</span>

			</view>
			<view class="sd">
				<u--input :placeholder="$t('Pleaseentercontent')" border="surround" v-model="FormData.city"
					:customStyle="{width:'650rpx',height:'49rpx'}" clearable></u--input>
			</view>
		</view>
		<view class="items">
			<view class="itemsTip">
				<span>*</span>
				<!-- <span>E-mail</span> -->
				<span>{{$t('newEmail.Email')}}</span>


			</view>
			<view class="sd">
				<u--input :placeholder="$t('Pleaseentercontent')" border="surround" v-model="FormData.email"
					:customStyle="{width:'650rpx',height:'49rpx'}" clearable></u--input>
			</view>
		</view>

		<view class="items" @click="dateshow = true">
			<view class="itemsTip">
				<span>*</span>
				<!-- <span>Birth</span> -->
				<span>{{$t('Dateofbirth')}}</span>

			</view>
			<view class="sd b">
				<span>{{FormData.birth ? FormData.birth : this.$t('Pleaseselectthedateofbirth')}}</span>
				<view class="icon">
					<u-icon name="arrow-down" v-if="dateshow == false"></u-icon>
					<u-icon name="arrow-up" v-else></u-icon>
				</view>
			</view>
			<u-datetime-picker :show="dateshow" v-model="date" :minDate="-631152000000" @confirm="dateconfirm" mode="date"
				:confirmText="$t('Confirm')" :cancelText="$t('Cancel')" @cancel="dateshow = false"
				:formatter="formatter"></u-datetime-picker>
		</view>

		<view class="items">
			<view class="itemsTip">
				<span>*</span>
				<span>{{$t('Gender')}}</span>

			</view>
			<view class="sd">
				<!-- 	<u--input placeholder="Please enter content" border="surround" v-model="FormData.gender"
					:customStyle="{width:'650rpx',height:'49rpx'}" clearable></u--input> -->

				<u-radio-group v-model="FormData.gender" placement="row">
					<u-radio :customStyle="{marginBottom: '8px'}" :label="$t('female')" :name="0">
					</u-radio>
					<u-radio :customStyle="{marginBottom: '8px',marginLeft:'60rpx'}" :label="$t('male')" :name="1">
					</u-radio>
				</u-radio-group>
			</view>
		</view>

		<view class="g">
			<u-button type="primary" :text="$t('ChangePassword.Confirm')"
				:customStyle="{width:'320rpx',height:'80rpx',backgroundColor:'#273562',color:'#fff',margin:'30rpx 0 0rpx',fontWeight:'700',border:'none',borderRadius:'10rpx'}"
				@click="submit"></u-button>
			<u-button type="primary" :text="$t('ChangePassword.Resetcontent')"
				:customStyle="{width:'320rpx',height:'80rpx',backgroundColor:'#fff',color:'#666',margin:'30rpx 0 0rpx',fontWeight:'700',border:'none',borderRadius:'10rpx',border:'2rpx solid #273562'}"
				@click="Set"></u-button>

		</view>
	</view>
</template>

<script>
import loginVue from '../index/login/login.vue'
	export default {
		data() {
			return {
				newUrl:1,//0为新接口1为老接口
				fileList1: [],
				avatarUrl:'',
				action: this.$baseUrl + "/backend/upload/uploadAvatar", //文件上传接口
				FormData: {
					...JSON.parse(uni.getStorageSync('user'))
				},
				dateshow: false,
				date: '',
			}
		},
		mounted() {
			let atu = JSON.parse(uni.getStorageSync('user')).avatar
			if(atu.startsWith('https://api.myonewow.com')) {
				this.avatarUrl = atu
			} else if(atu.startsWith('images/user/avatar/')) {
				this.avatarUrl = `${this.$baseUrl}/${atu}`
			}
			this.fileList1.push({
				url:this.avatarUrl
			})
			console.log(this.fileList1,'fileList1fileList1');
		},
		methods: {
			// 选择出生年月日
			dateconfirm(e) {
				let date = new Date(e.value)
				let year = date.getFullYear()
				let month = date.getMonth() + 1
				let day = date.getDate()
				this.FormData.birth = `${year}-${month}-${day}`
				this.dateshow = false
			},
			// 重置表单
			Set() {
				this.FormData = {}
			},
			// 提交数据
			submit() {
				// if (!this.FormData.real_name) {
				// 	this.$ToastFail(this.$t('Usernametpss'))
				// 	return
				// }
				if(this.fileList1.length > 0) {
					if(this.fileList1[0].url.url) {
						this.FormData.avatar = this.fileList1[0].url.url
					}
				}
				if (!this.FormData.lastname) {
					this.$ToastFail(this.$t('LastNameisrequired’'))
					return
				}

				if (!this.FormData.firstname) {
					this.$ToastFail(this.$t('FirstNameisrequired'))
					return
				}

				if (!this.FormData.country) {
					this.$ToastFail(this.$t('Pleaseselectacountry'))
					return
				}

				if (!this.FormData.address) {
					this.$ToastFail(this.$t('addressisrequired'))
					return
				}
				if (!this.FormData.address2) {
					this.$ToastFail(this.$t('Aaddress2sss'))
					return
				}
				if (!this.FormData.state) {
					this.$ToastFail(this.$t('AddNewPolicy.State.tips'))
					return
				}
				if (!this.FormData.city) {
					this.$ToastFail(this.$t('Cityisrequired'))
					return
				}
				if (!this.FormData.birth) {
					this.$ToastFail(this.$t('Pleaseselectthedateofbirth'))
					return
				}


				uni.showLoading({
					title: this.$t('Laoding'),
				})
				this.$post(`/backend/user/editUserInfo`, this.FormData,this.newUrl).then(res => {
					if (res.status) {
						this.$ToastOk(`${res.data.msg}`)
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/Profile/Profile'
							})
						}, 1000)
					} else {
						this.$ToastFail(`${res.message}`)
					}
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					let a = 0;
					if (event.name == 1) {
						a = 1;
					}
					if (event.name == 2) {
						a = 2;
					}
					const result = await this.uploadFilePromise(lists[i].url, a)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url, type) {
				uni.showLoading({
					mask: true
				})
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.action, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'image',
						header: {
							token:uni.getStorageSync('token'),
							authorization: `Bearer ${uni.getStorageSync('token')}`,
							'content-type':'multipart/form-data'
						},
						success: (res) => {
							uni.hideLoading()
							return resolve(JSON.parse(res.data).data)
						},
						error: () => {
							uni.hideLoading()
						}
					});
				})
			},

		}
	}
</script>

<style>
	.pages {
		padding: 40rpx 40rpx 80rpx;
		width: 100%;
		height: 100vh;
		overflow-y: auto;
		box-sizing: border-box;
	}

	.pages_title {
		font-family: Roboto;
		font-size: 24px;
		font-weight: 500;
		line-height: 24px;
		letter-spacing: 0em;
		text-align: left;
		/* margin-top: 40rpx; */
		margin: 40rpx 0 108rpx;
	}

	.pages_tips {
		font-family: Roboto;
		font-size: 18px;
		font-weight: 500;
		line-height: 22px;
		letter-spacing: 0em;
		text-align: left;
		margin-top: 168rpx;
	}

	.items {
		display: flex;
		align-items: flex-start;
		justify-content: center;
		flex-direction: column;
		width: 100%;
		height: 160rpx;
		margin: 2rpx auto 0;
		/* padding: 0 36rpx; */
		box-sizing: border-box;
	}

	.itemsTip {
		display: flex;
		align-items: center;
		height: 54rpx;
		font-family: Roboto;
		font-size: 32rpx;
		font-weight: 500;
		line-height: 40rpx;
		letter-spacing: 0em;
		text-align: left;

	}

	.itemsTip span:first-child {
		color: red;
		/* padding-top: 5rpx; */
		line-height: 5rpx;
		margin-right: 6rpx;
		padding-top: 10rpx;
	}

	.sd {
		display: flex;
		/* justify-content: flex-start; */
		align-items: center;
		width: 100%;
		height: 80rpx;
		/* background-color: red; */
		border-radius: 10rpx;
	}

	.b {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-family: Roboto;
		font-size: 28rpx;
		font-weight: 400;
		letter-spacing: 0em;
		text-align: left;
		border: 2rpx solid #ccc;
		height: 74rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.g {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 40rpx;
		box-sizing: border-box;
	}

	.b {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-family: Roboto;
		font-size: 28rpx;
		font-weight: 400;
		letter-spacing: 0em;
		text-align: left;
		border: 2rpx solid #ccc;
		height: 74rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
	}
	.touxiang {
		/* padding: 40rpx 0 0 40rpx; */
	}
</style>